package org.aiforum.web.action;


import org.aiforum.domain.Reply;
import org.aiforum.domain.Topic;
import org.aiforum.domain.User;
import org.aiforum.frountend.common.Response;
import org.aiforum.frountend.common.Responsehelper;
import org.aiforum.frountend.common.UserHelper;
import org.aiforum.web.common.AiActionContext;
import org.apache.struts2.ServletActionContext;

/**
 * 用户请求Action
 * @author VIC
 *
 */
public class UserAction extends AiActionContext {
	
	private static final long serialVersionUID = 1L;

	private User user = new User();


	private Topic topic = new Topic();
	private Reply reply = new Reply();
	
	/**
	 * 用户回帖
	 */
	public void reply_topic(){
		// 设置当前回帖用户ID和账号
		String token = getCookie("token");
		reply.setUserId(UserHelper.getCurrentUserId(token));
		reply.setAccount(UserHelper.getCurrentAccount(token));
		Response responseObj = replyService.save(reply);
		Responsehelper.print("message?message="+ responseObj.getMessage(),ServletActionContext.getResponse());
	}
	
	/**
	 * 用户发帖
	 * @return
	 */
	public void post_topic(){	
		
		// 设置当前发帖用户ID和账号
		String token = getCookie("token");
		topic.setUserId(UserHelper.getCurrentUserId(token));
		topic.setAccount(UserHelper.getCurrentAccount(token));
		Response responseObj = topicService.post(topic);
		Responsehelper.print("message?message="+ responseObj.getMessage(),ServletActionContext.getResponse());
	}
	
	/**
	 * 用户登出
	 * @return
	 */
	public String logout(){
		// 获取当前用户名
		String token = getCookie("token");
		// 移除用户登录缓存
		userService.logout(token);
		// 注销Session
		invalidateSession();
		// 移除token
		delCookie("token");
		return "index";
	}
	
	/**
	 * 处理用户登录请求
	 * @return
	 */
	public String login(){
		
		// 调用用户业务接口进行登录
		Response response = userService.login(user);
		if(response.isState()){
			// 将Token添加到Cookie中
			setCookie("token", response.getData().toString());
			logger.info(user.getAccount() + " login success.");
			// 返回登录成功进入首页
			return "index";	
		}else{
			// 登录失败返回登录页面
			// 将消息添加到 struts2上下文
			logger.warn(user.getAccount() + " login fail." + response.getMessage());
			addActionError(response.getMessage());
			return "login";
		}
		
	}
	
	/**
	 * 处理注册用户请求
	 * @return
	 */
	public String register(){
		
		// 调用用户业务接口进行注册
		Response response = userService.register(user);
		if(response.isState()){
			logger.info(user.getAccount() + " register success.");
			// 返回注册成功进入登录页面
			return "login";	
		}else{
			// 注册失败返回注册页面
			// 将消息添加到 struts2上下文
			logger.info(user.getAccount() + " login fail. " + response.getMessage());
			addActionError(response.getMessage());
			return "register";
		}
		
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}

	public Reply getReply() {
		return reply;
	}

	public void setReply(Reply reply) {
		this.reply = reply;
	}
	
}
